import UIKit

class TF_LivingInvitationListView: UIView, UITableViewDelegate , UITableViewDataSource{
    
    @objc public var list = NSMutableArray()
    @objc public let tableView = UITableView()
    @objc public var isShowView:Bool = false

    let view = UIView()
    let title = UILabel()
    let lineView = UIView()

    override init(frame: CGRect) {
        super.init(frame: frame)
  
        self.addSubview(self.view)
        self.view.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
     
        self.title.numberOfLines = 0;
        self.view.addSubview(self.title)
        self.title.textAlignment = .center
        self.title.text = "邀请榜单";
        self.title.font = UIFont.PingFang(size: 16)
    
        self.title.textColor = UIColor(red: 35/255.0, green: 57/255.0, blue: 64/255.0, alpha: 1.0)
    
        //分割线
        self.view.addSubview(self.lineView)
        self.lineView.backgroundColor = UIColor(red: 235/255.0, green: 239/255.0, blue: 242/255.0, alpha: 1.0)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.view.addSubview(self.tableView)
        self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.tableView.register(TF_InvitationListViewCell.self, forCellReuseIdentifier: "TF_InvitationListViewCell")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TF_InvitationListViewCell", for: indexPath) as! TF_InvitationListViewCell
        cell.selectionStyle = .none
        let model = self.list[indexPath.row] as! TFInvitationListMOdel
        cell.model = model
        
        if (indexPath.row < 3) {
            cell.rankingImage.isHidden = false
            cell.ranking.isHidden = true
            if (indexPath.row == 0) {
                cell.rankingImage.image = UIImage.init(named: "HTLive.bundle/bangjin")
            
            }else if (indexPath.row == 1) {
                cell.rankingImage.image = UIImage.init(named: "HTLive.bundle/bangyin")
       
            }else if (indexPath.row == 2 ) {
                cell.rankingImage.image = UIImage.init(named: "HTLive.bundle/bangtong")
            
            }
           
        }else{
            cell.rankingImage.isHidden = true
            cell.ranking.isHidden = false
            
            let text:NSString = NSString.init(format: "%li", indexPath.row+1)
            
            cell.ranking.text = text as String
        
        }
       
        return cell
    }
    // 设置cell高度
       func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
           return 35+13
       }

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        if (self.isUserInteractionEnabled == false || self.isHidden == true || self.alpha <= 0.01) {
            return nil
        }
     
        if (self.point(inside: point, with: event ) ) {
            // 3.从后往前遍历自己的子控件
            let count = self.subviews.count as Int

            for i in 0..<count {
                // 取出子控件
               let childV = self.subviews[i] as UIView
                // // 把自己坐标系上点转化成子控件坐标系上点
               
                let childP = self.convert(point, to: childV)
              
                let fitView = childV.hitTest(childP, with: event)
                if (fitView != nil) {
                    return fitView
                }
            }
        }
        if (self.isShowView) {
            self.showView(show: false)
        }
        return nil;
    }
    
   @objc public func setUI()
   
    {
        self.view.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 346+44)
   
        
        self.title.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 44)
     
        self.lineView.frame = CGRect(x: 0, y: self.title.frame.maxY, width: self.view.frame.size.width, height: 0.5)
        
        let tableViewY = self.lineView.frame.maxY+10
        self.tableView.frame = CGRect(x: 0, y: tableViewY, width: self.view.frame.size.width, height: self.frame.size.height-tableViewY)
   
        
        let maskeight:CGFloat = 12
        let maskPath = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners:[UIRectCorner.topRight,UIRectCorner.topLeft], cornerRadii: CGSize(width: maskeight, height: maskeight))
        
        let  maskLayer = CAShapeLayer()
        maskLayer.frame =  self.view.bounds;
        maskLayer.path = maskPath.cgPath
   
        self.view.layer.mask = maskLayer;

    }
    @objc public func showView(show:Bool)
    {
        self.isShowView = show;
        UIView.animate(withDuration: 0.5) {
            if((self.superview) != nil)
            {
                if (show) {
                    self.view.frame = CGRect(x: 0, y: self.frame.size.height-346-44, width: self.frame.size.width, height: 346+44)
                }else{
                    self.view.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 346 + 44)
                    
                }
            }
            
        }
    }
}
